'''
Created on 2011-03-30

@author: jasisz
'''
import yaml

class Resource(yaml.YAMLObject):
    yaml_tag = u'!Resource'
    def __init__(self, name = ""):
        self.name = name
        self.properties = {}
        
    def setProperty(self, key, value):
        """
        adds or sets property given by key and value
        """
        self.properties[key] = value   
        
    def setType(self, typeName):
        self.type = typeName 
    
    def delProperty(self, key):
        del self.properties[key]
        
    def __repr__(self):
        return "%s(name=%r, properties=%r)" % (self.__class__.__name__, self.name, self.properties)
        
class Field(yaml.YAMLObject):
    yaml_tag = u'!Field'
    def __init__(self, name = ""):
        self.name = name
        self.resources = []
    
    def addResource(self, res):
        self.resources.append(res)
        
    def removeResourceByName(self, name):
        for i in self.resources:
            if i.name == name:
                self.resources.remove(i)
        
    def __getitem__(self, index):
        return self.resources[index]
    
    def __repr__(self):
        return "%s(name=%r, resources=%r)" % (self.__class__.__name__, self.name, self.resources)
    
class Action(yaml.YAMLObject):
    yaml_tag = u'!Action'
    def __init__(self, namefrom, nameto, what):
        self.fieldFrom = namefrom
        self.fieldTo = nameto
        self.what = what
        
    def __repr__(self):
        return "%s(from=%r, to=%r, what=%r)" % (self.__class__.__name__, self.fromm, self.to, self.what)

class Rule(yaml.YAMLObject):
    yaml_tag = u'!Rule'
    def __init__(self, name = ""):
        self.name = name
        self.cost = []
        self.todo = []
        self.conditions = []
        
    def __repr__(self):
        return "%s(cost=%r, todo=%r)" % (self.__class__.__name__, self.cost, self.todo)
            
class Game(yaml.YAMLObject):
    yaml_tag = u'!Game'
    def __init__(self, name):
        self.name = name
        self.fields = []
        self.rules = []
        
    def __getitem__(self, index):
        return self.fields[index]
    
    def addField(self, field):
        self.fields.append(field)
        
    def addRule(self, rule):
        self.rules.append(rule)
    
    def __repr__(self):
        return "%s(name=%r, fields=%r, rules=%r)" % (self.__class__.__name__, self.name, self.fields, self.rules)
    
if __name__ == "__main__":
    res = Resource("sraka")
    #res.setProperty("fajans", "dupka")
    #res.setProperty("dsaa", "fds")
    field = Field("Pole sobie jestem")
    field.addResource(res)
    res2 = Resource("dupa")
    res2.setProperty("fajansik", "woda")
    field.addResource(res2)
    game = Game("Mega wypasiona gra sromotna")
    game.addField(field)
    print("Pierwsza klasa")
    print(yaml.dump(game, default_flow_style=False))
    
    game2 = yaml.load("""!Game
name: Mega wypasiona gra sromotna
fields:
- !Field
  name: Pole sobie jestem
  resources:
  - !Resource
    name: sraka
    properties:
      fajans: dupka
  - !Resource
    name: dupa
    properties:
      fajansik: woda
  - !Resource
    name: dada
    properties: {}
    type: dddd
""")
    print("Druga powinna byc taka sama")
    print(yaml.dump(game2, default_flow_style=False))
    